package cnhy.salary.controller;

import cn.hutool.core.date.DateUtil;
import cnhy.salary.domain.AttendanceRecord;
import cnhy.salary.domain.M3callbackRecord;
import cnhy.salary.domain.dto.M3HandlerDto;
import cnhy.salary.service.M3HandlerSvc;
import com.ruoyi.common.annotation.Anonymous;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import ling.common.DataUtil;
import ling.common.EQueryController;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


@Api(tags = "M3考勤机 功能接口")
@RestController
@RequestMapping("/salary/M3Handler")
@Slf4j
public class M3HandlerController extends EQueryController<AttendanceRecord> {
    @ApiOperation("回调")
    @Anonymous
    @RequestMapping(value = "/m3Callback",method = {RequestMethod.GET,RequestMethod.POST})
    @Transactional
    public M3HandlerDto.Result m3Callback(M3HandlerDto.QueryParam queryParam,@RequestBody M3HandlerDto body, HttpServletRequest request, HttpServletResponse response) {
        log.info("调用m3");
        var qb = new M3HandlerDto.QueryBody();
        qb.setBody(body);
        qb.setQuery(queryParam);
        var record = new M3callbackRecord();
        record.setCreatedTime(new Date());
        record.setOrgId(queryParam.getOrgId());

        record.setCallbackTag(body.getCallbackTag());
        record.setRecognizeTime(DateUtil.date(body.getData().getRecognizeTime()).toJdkDate());
        record.setTpId(body.getData().getTpId());
        record.setPersonId(body.getData().getPersonId());
        record.setData1(DataUtil.toJson(qb));
        queryHelper.entityQuery().insertable(record).executeRows(true);

        m3HandlerSvc.AttendanceRecordUpdate(record);
        return M3HandlerDto.Result.ok("完成");
    }

    @Resource
    M3HandlerSvc m3HandlerSvc;

    @ApiOperation("test")
    @Anonymous
    @RequestMapping(value = "/test",method = {RequestMethod.GET,RequestMethod.POST})
    public void  test(){
        //m3HandlerSvc.createEmpl();

    }
}
